package com.gtl.meishi.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.gtl.meishi.R;
import com.gtl.meishi.bean.Collection;
import com.gtl.meishi.dao.CollectionDao;

public class DetailActivity extends AppCompatActivity {
    private VideoView mVideoView;
    private TextView mTextViewTitle;
    private Collection collection=null;
    private CollectionDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        dao=new CollectionDao(Collection.class,this);
        mVideoView= (VideoView) findViewById(R.id.videoview_detail);
        mTextViewTitle= (TextView) findViewById(R.id.textview_title_detail);
        Intent in=getIntent();
        String video=in.getStringExtra("video");
        String caption=in.getStringExtra("caption");
        collection=new Collection();
        collection.setCaption(caption);
        collection.setVideo(video);
        mVideoView.setVideoURI(Uri.parse(video));
        mTextViewTitle.setText(caption);
        mVideoView.start();
        mVideoView.setMediaController(new MediaController(DetailActivity.this));




    }

    public void  doClick(View view){
        switch (view.getId()){
            case R.id.textview_collection_detail:
                if (collection!=null){
                    int flag=   dao.getByClum("video",collection.getVideo());
                    if(flag<1) {

                        int state = dao.add(collection);
                        if (state == -1) {
                            toast("收藏失败");
                        } else {
                            toast("收藏成功");

                        }
                    }else {
                        toast("已经收藏过");
                    }
                }




                break;
        }
    }

    public  void  toast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();

    }
}
